define(['jquery', 'bootstrap', 'backend', 'table', 'form','layer'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'yalimedia/coursequestioncategory/index?ids='+$("#table").attr('data-id'),
                    multi_url: 'yalimedia/coursequestioncategory/multi',
                    add_url: 'yalimedia/coursequestioncategory/add?ids='+$("#table").attr('data-id'),
                    edit_url: 'yalimedia/coursequestioncategory/edit',
                    del_url: 'yalimedia/coursequestioncategory/del',
                    table: 'yalimedia_course_question_category',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                sortOrder:'asc',
                columns: [
                    [
                        {checkbox: true},
                        {
                            field: 'id',
                            title: __('ID'),
                            formatter: Table.api.formatter.normal,
                            align: 'left',
                        },
                        {
                            field: 'name',
                            title: __('分类名称'),
                            formatter: Table.api.formatter.normal,
                            align: 'left',
                        },
                        {field: 'operate', title: __('Operate'), width:350, align:'right', table: table, events: Table.api.events.operate,align: 'left',
                            buttons: [
                                {
                                    // visible:function (row) {
                                    //     if(row['pid'] == "0"){
                                    //         return false;
                                    //     }else{
                                    //         return true;
                                    //     }
                                    // },
                                    name: 'content',
                                    extend:'data-area=["90%","90%"]',
                                    title: __('试题列表'),
                                    text:'试题列表',
                                    classname: 'btn btn-xs btn-default btn-dialog',
                                    icon: 'fa',
                                    url: 'yalimedia/coursequestion/index'
                                },
                                {
                                    // visible:function (row) {
                                    //     if(row['pid'] == "0"){
                                    //         return false;
                                    //     }else{
                                    //         return true;
                                    //     }
                                    // },
                                    name: 'content',
                                    extend:'data-area=["90%","90%"]',
                                    title: __('添加试题'),
                                    text:'添加试题',
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    icon: 'fa',
                                    url: 'exam/question/select',
                                    callback: (data) => {
                                        
                                        var id = '';
                                        for (let i = 0; i < data.length; i++) {
                                            if( i == 0){
                                                id += data[i].id;
                                            }else{
                                                id += ','+data[i].id;
                                            }
                                        }

                                        Fast.api.ajax({
                                            url: 'yalimedia/course/add_question?course_id='+$('#table').attr('data-course_id'),
                                            data: {
                                                'id':id
                                            },
                                            success: function(data) {
                                                Layer.closeAll();
                                                Toastr.success('添加成功');
                                            }
                                        });
                                    }
                                }],formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        sort: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});